package com.steven.test.web.controller;

import com.steven.test.enums.ResponseEnum;
import com.steven.test.exception.GlobalException;
import com.steven.test.web.request.BasicRequest;
import com.steven.test.web.response.ResponseData;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Map;

/**
 * Restful 接口风格
 *
 * @author qinxh
 * @date 2019/06/24
 */
@RestController
@RequestMapping("/restful")
public class RestfulController{

    /**
     * 获取所有
     *
     * @return ResponseData
     */
    @GetMapping("")
    public ResponseData getList(){

        return ResponseData.ofSuccess("getList");
    }

    /**
     * 分页获取数据（从url中获取参数）
     *
     * get http://127.0.0.1:8080/restful/1/10/page
     *
     * @param pageNum                       页码
     * @param pageSize                      分页大小
     * @return ResponseData
     */
    @GetMapping("/{page}/{size}/page")
    public ResponseData getPageList(@PathVariable("page") Integer pageNum,@PathVariable("size") Integer pageSize){

        return ResponseData.ofSuccess("page:"+pageNum+",size:"+pageSize); 
    }

    /**
     * 获取具体数据
     *
     * get http://127.0.0.1:8080/restful/1
     *
     * @return ResponseData
     */
    @GetMapping("/{id}")
    public ResponseData getRestful(@PathVariable("id") Long id){
    	
        return ResponseData.ofSuccess("获取某个数据，id为"+id);
    }

    /**
     * 根据参数过滤查询结果（PathVariable方式，不指定参数名）
     *
     * get http://127.0.0.1:8080/restful/1/hhaha/23/basic
     *
     * @param map                       PathVariable方式请求map
     * @return ResponseData
     */
    @GetMapping("/{id}/{name}/{age}/basic")
    public ResponseData getRestfulByPathVariable(@PathVariable Map<String,Object> map){

        return ResponseData.ofSuccess("获取数据为："+map);
    }

    /**
     * 根据参数过滤查询结果（使用url参数）
     *
     * get http://127.0.0.1:8080/restful/param?param=123
     *
     * @param param                     url参数中的参数名
     * @return ResponseData
     */
    @GetMapping("/param")
    public ResponseData getRequest(@RequestParam("param") String param){

        return ResponseData.ofSuccess(param);
    }

    /**
     * 通过对象接收
     *
     * get http://127.0.0.1:8080/restful/param?param=123
     *
     * @param basicRequest              请求体
     * @return ResponseData
     */
    @GetMapping("/basicRequest")
    public ResponseData getRequestByBasicRequest(BasicRequest basicRequest){

        return ResponseData.ofSuccess(basicRequest);
    }

    /**
     * 根据参数过滤查询结果（不指定参数名）
     *
     * get http://127.0.0.1:8080/restful/map?id=123&name=liuyu&age=23
     *
     * @param map                       参数map
     * @return ResponseData
     */
    @GetMapping("/map")
    public ResponseData getRequestByMap(@RequestParam Map<String,Object> map){

        return ResponseData.ofSuccess(map);
    }

    /**
     * 新增数据（使用json对象方式）
     *
     * post http://127.0.0.1:8080/restful
     * header  Content-Type:application/json;charset=utf-8
     * data
     *  {
     *      "id": "123",
     *      "name": "liuyu",
     *      "age": "23"
     *  }
     *
     * @param basicRequest                      请求体
     * @return ResponseData
     */
    @PostMapping("")
    public ResponseData postRestful(@RequestBody BasicRequest basicRequest){

       return ResponseData.ofSuccess(basicRequest);
    }

    /**
     * 新增数据(使用表单form-data方式)
     *
     * post http://127.0.0.1:8080/restful/form
     * header 不需要指定Content-Type
     * data
     * {
     *  name liuyu
     *  age  23
     * }
     *
     * @param basicRequest                      表单提交的请求体
     * @return ResponseData
     */
    @PostMapping("/from")
    public ResponseData postByFormRestful(BasicRequest basicRequest){

        return ResponseData.ofSuccess(basicRequest);
    }

    /**
     * 更新某个数据
     *
     * @param id                                id
     * @param basicRequest                      json请求体
     * @return ResponseData
     */
    @PutMapping("/{id}")
    public ResponseData putRestful(@PathVariable("id") Long id,@RequestBody BasicRequest basicRequest){

        return ResponseData.ofSuccess("id is:"+id+";RequestBody is:"+basicRequest);
    }

    /**
     * 删除某个数据
     *
     * @param id                                id
     * @return ResponseData
     */
    @DeleteMapping("/{id}")
    public ResponseData deleteRestful(@PathVariable("id") Long id){

        return ResponseData.ofSuccess(id+" delete successful!");
    }


    /**
     * 模拟WebClient读取HttpServletResponse中数据
     *
     * @param response                      返回
     */
    @PostMapping("/download/")
    public void downloadFile(HttpServletResponse response) {

        try (OutputStream os = response.getOutputStream()){

            byte[] resultBytes = {1,21,31,41,1,1,3,24,4,5,61,31,41,1,1,3,24,4,5,6,1,1,1,1,21,1,33,2,43,5,64,1,14,3,52,4,1,1,1,1,21,1,33,2,43,5,64,1,14,3,52,4,5,6};
            os.write(resultBytes);
            os.flush();

        } catch (Exception e) {
            throw new GlobalException(ResponseEnum.MULTIPART_ERROR);
        }
    }

}